package com.cbn.ibcp.jishiting.rms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbn.ibcp.common.annotation.DataSource;
import com.cbn.ibcp.common.enums.DataSourceType;
import com.cbn.ibcp.jishiting.rms.domain.ResBase;
import com.cbn.ibcp.jishiting.rms.enums.ResBaseOssInjectStatusEnum;
import com.cbn.ibcp.jishiting.rms.mapper.ResBaseMapper;
import com.cbn.ibcp.jishiting.rms.service.IResBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class ResBaseServiceImpl implements IResBaseService {


    private final ResBaseMapper resBaseMapper;


    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public Integer getTotalCreateInjectStatistic(String tenantCode, Date startTime, Date endTime, Integer injectStatus) {
        log.info("getTotalCreateInjectStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(ResBase::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(ResBase::getCreateTime, endTime);
        }
        if (Objects.nonNull(injectStatus)) {
            queryWrapper.eq(ResBase::getInjectStatus, injectStatus);
        }
        return resBaseMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public Integer getTotalCreateInjectFailureStatistic(String tenantCode, Date startTime, Date endTime) {
        log.info("getTotalCreateInjectFailureStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(ResBase::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(ResBase::getCreateTime, endTime);
        }
        queryWrapper.notIn(ResBase::getInjectStatus, ResBaseOssInjectStatusEnum.WAITING_INJECT.getCode(), ResBaseOssInjectStatusEnum.INJECTING.getCode(), ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        return resBaseMapper.selectCount(queryWrapper);
    }


    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public Integer getTotalUpdateInjectStatistic(String tenantCode, Date startTime, Date endTime, Integer injectStatus) {
        log.info("getTotalUpdateInjectStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(ResBase::getUpdateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(ResBase::getUpdateTime, endTime);
        }
        if (Objects.nonNull(injectStatus)) {
            queryWrapper.eq(ResBase::getInjectStatus, injectStatus);
        }
        return resBaseMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public Integer getTotalUpdateInjectFailureStatistic(String tenantCode, Date startTime, Date endTime) {
        log.info("getTotalUpdateInjectFailureStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(ResBase::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(ResBase::getCreateTime, endTime);
        }
        queryWrapper.notIn(ResBase::getInjectStatus, ResBaseOssInjectStatusEnum.WAITING_INJECT.getCode(), ResBaseOssInjectStatusEnum.INJECTING.getCode(), ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        return resBaseMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public List<ResBase> getListByIdList(List<Long> idList) {
        log.info("getListByIdList param, idList:{}", JSONUtil.toJsonStr(idList));
        if (CollectionUtil.isEmpty(idList)) {
            return null;
        }
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ResBase::getId, idList);
        return resBaseMapper.selectList(queryWrapper);
    }

    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public List<ResBase> getListByIdListAndResTypeList(List<Long> idList, List<Integer> resTypeList) {
        log.info("getListByIdListAndResTypeList param, idList:{},resTypeList:{}", JSONUtil.toJsonStr(idList), JSONUtil.toJsonStr(resTypeList));
        if (CollectionUtil.isEmpty(idList) || CollectionUtil.isEmpty(resTypeList)) {
            return null;
        }
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ResBase::getId, idList);
        queryWrapper.in(ResBase::getResType, resTypeList);
        return resBaseMapper.selectList(queryWrapper);
    }

    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public ResBase getOneByIdAndResType(String tenantCode, String orgCode, Long id, Integer resType) {
        if (StrUtil.isBlank(orgCode) || Objects.isNull(id) || Objects.isNull(resType)) {
            return null;
        }
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResBase::getId, id);
        queryWrapper.eq(ResBase::getResType, resType);
        return resBaseMapper.selectOne(queryWrapper);
    }


    @DataSource(DataSourceType.ibcp_rms)
    @Override
    public ResBase getOneAlbumById(String tenantCode, String orgCode, Long id) {
        if (StrUtil.isBlank(orgCode) || Objects.isNull(id)) {
            return null;
        }
        LambdaQueryWrapper<ResBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResBase::getId, id);
        queryWrapper.in(ResBase::getResType, ListUtil.toList(1, 2));
        return resBaseMapper.selectOne(queryWrapper);
    }
}
